package com.example.workmanagerdemo

import android.content.Context
import android.util.Log
import androidx.work.Worker
import androidx.work.WorkerParameters
import androidx.work.workDataOf

//杀掉应用程序，任务是无法执行的。 只有等应用再次被开启的时候才会继续执行。
//一般的退出，是会执行的。
class MyWork(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
    override fun doWork(): Result {
        val name = inputData.getString(INPUT_DATA_KEY)

        Log.e("MyWork", "dowork start $name")
        Thread.sleep(5000)
        Log.e("MyWork", "dowork stop $name")

        val sharedPreferences =
            applicationContext.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
        var number:Int = sharedPreferences.getInt(name, 0)
        sharedPreferences.edit().putInt(name,++number).apply()
        return Result.success(workDataOf(OUTPUT_DATA_KEY to "$name"));

    }
}